package com.geekgo.ladder.core.base;

import com.baomidou.mybatisplus.plugins.Page;
import com.geekgo.ladder.core.util.InstanceUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.ContextLoader;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author geekgo
 * @version 2016/12/21 11:12
 */
public abstract class BaseProviderImpl<T extends BaseModel> implements BaseProvider<T>{

    protected final Logger LOG = LogManager.getLogger(getClass());
    @Autowired
    protected BaseMapper<T> mapper;


    private BaseProviderImpl<T> getProvider(){
        return InstanceUtil.getBean(getClass());
    }

    @Transactional
    @Override
    public T update(T record) {
        try {
            record.setEnable(true);
            record.setUpdateTime(new Date());
            if(record.getId()==null){
                record.setCreateTime(new Date());
                mapper.insert(record);
            }else{
                mapper.updateById(record);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
        return record;
    }

    @Override
    public void delete(Long id, Long userId) {
        try {
            T record = queryById(id);
            if(record!=null){
                record.setEnable(false);
                record.setUpdateBy(userId);
                record.setUpdateTime(new Date());
                mapper.updateById(record);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }

    @Override
    public T queryById(Long id) {
        T record = null;
        try {
            record = mapper.selectById(id);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(),e);
        }
        return record;
    }

    /**
     * 分页查询(获取分页信息)
     * @param params
     * @return
     */
    protected Page<Long> getPage(Map<String,Object> params){
        Integer current = 1;
        Integer size = 10;
        String orderBy = "id_";
        String pageNumStr = (String)params.get("pageNum");
        String pageSizeStr = (String)params.get("pageSize");
        String orderByStr = (String)params.get("orderBy");
        if(StringUtils.isNotBlank(pageNumStr)){
            current = Integer.parseInt(pageNumStr);
        }
        if(StringUtils.isNotBlank(pageSizeStr)){
            size = Integer.parseInt(pageSizeStr);
        }
        if(StringUtils.isNotBlank(orderByStr)){
            orderBy =orderByStr;
        }
        Page<Long> page = new Page<Long>(current,size,orderBy);
        page.setAsc(false);
        return page;
    }

    @Override
    public Page<T> query(Map<String, Object> params) {
        return null;
    }

    /**
     *
     * @param ids
     * @return
     */
    public Page<T> getPage(Page<Long> ids){
        if(ids!=null){
            Page<T> page = new Page<T>(ids.getCurrent(),ids.getSize());
            page.setTotal(ids.getTotal());
            List<T> records = InstanceUtil.newArrayList();
            for(Long id:ids.getRecords()){
                T record = this.queryById(id);
                if(record!=null){
                    records.add(record);
                }
            }
            page.setRecords(records);
        }
        return new Page<T>();
    }
    @Override
    public <K> Page<K> getPage(Page<Long> ids, Class<K> cls) {
        if(ids!=null){
            Page<K> page = new Page<K>(ids.getCurrent(),ids.getSize());
            page.setTotal(ids.getTotal());
            List<K> records = InstanceUtil.newArrayList();
            for(Long id:ids.getRecords()){
                T record = this.queryById(id);
                if(record!=null){
                    K k = InstanceUtil.to(record,cls);
                    records.add(k);
                }
            }
            page.setRecords(records);
        }
        return new Page<K>();
    }

    /**
     * 根据id查询(默认类型T)
     * @param ids
     * @return
     */
    public List<T> getList(List<Long> ids){
        List<T> list = InstanceUtil.newArrayList();
        if(ids!=null){
            for(Long id:ids){
                T record = this.queryById(id);
                if(record!=null){
                    list.add(record);
                }
            }
        }
        return list;
    }

    /**
     * 根据id查询(返回指定类型Class)
     * @param ids
     * @param cls
     * @param <K>
     * @return
     */
    @Override
    public <K> List<K> getList(List<Long> ids, Class<K> cls) {
        List<K> list = InstanceUtil.newArrayList();
        if(ids!=null){
            for(Long id:ids){
                T record = this.queryById(id);
                if(record!=null){
                    K k = InstanceUtil.to(record,cls);
                    list.add(k);
                }
            }
        }
        return list;
    }
}
